<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    var removeNthFromEnd = function (head, n) {
      let dummy = new ListNode(0, head);          //用dummy节点统一头结点和非头结点的处理方式
      let fast = slow = dummy;                    //定义快慢指针
      while (n--) {
        fast = fast.next;                       //快指针向后移动n个
      }
      while (fast.next !== null) {                //俩指针同时向后移n个单位
        fast = fast.next;
        slow = slow.next;
      }
      slow.next = slow.next.next;                 //删除第n个 本质就是跳过第n个
      return dummy.next;
    };
    console.log(removeNthFromEnd(head = [1, 2, 3, 4, 5], n = 2))
  </script>
</body>

</html>